package kickboxing.view;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import kickboxing.controller.InformacjeManager;
import kickboxing.model.Informacje;
import kickboxing.model.Uzytkownik;

import org.apache.commons.beanutils.BeanUtils;

/**
 * Servlet implementation class InformacjeAction
 */
public class InformacjeAction extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public InformacjeAction() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html; charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        
        Informacje info = new Informacje();
        InformacjeManager linkController = new InformacjeManager();
        HttpSession sesja = request.getSession(true);
        Uzytkownik user = (Uzytkownik) sesja.getAttribute("user");
        if(user != null){
        	
        	try {
				BeanUtils.populate(info, request.getParameterMap());
				
					linkController.zapisz(info, user);
				
				 String message = "Dodano Informacje!";
				request.setAttribute("message", message);
                String x = "message="+message;
                
                response.sendRedirect("index.jsp?web=mojprofil");
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
        }
	}

}
